package org.celery.mobile.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.celery.mobile.vo.GoodsCategoryVo;
import org.celery.mobile.vo.GoodsSpecificationVo;
import org.celery.mobile.vo.GoodsVo;
import org.celery.modules.goods.entity.*;
import org.celery.modules.goods.service.*;
import org.celery.modules.merchant.entity.Merchant;
import org.celery.modules.merchant.service.IMerchantService;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Create on 2021-12-13
 *
 * @author Celery <1031868928@qq.com>
 */
@RestController
@AllArgsConstructor
@RequestMapping("/mobile/goods")
public class MobileGoodsController {

    private final IGoodsService goodsService;
    private final IGoodsCategoryService goodsCategoryService;
    private final IGoodsLabelService goodsLabelService;
    private final IGoodsSpecificationService goodsSpecificationService;
    private final IGoodsSpecificationDataService goodsSpecificationDataService;
    private final IMerchantService merchantService;

    @GetMapping(value = "list")
    public R<List<GoodsCategoryVo>> list(String merchantId) {
        Merchant merchant = merchantService.getById(merchantId);
        if (Func.isEmpty(merchant)) {
            throw new ServiceException("商户不存在");
        }
        List<GoodsCategory> goodsCategoryList = goodsCategoryService.list(Wrappers.<GoodsCategory>lambdaQuery()
                .in(GoodsCategory::getTenantId, new ArrayList<String>() {{
                    add(BladeConstant.ADMIN_TENANT_ID);
                    add(merchant.getTenantId());
                }})
        );
        List<Goods> goodsList = goodsService.list(Wrappers.<Goods>lambdaQuery()
                .in(Goods::getTenantId, new ArrayList<String>() {{
                    add(BladeConstant.ADMIN_TENANT_ID);
                    add(merchant.getTenantId());
                }})
        );
        List<GoodsLabel> goodsLabelList = goodsLabelService.list(Wrappers.<GoodsLabel>lambdaQuery()
                .in(GoodsLabel::getTenantId, (new ArrayList<String>() {{
                    add(BladeConstant.ADMIN_TENANT_ID);
                    add(merchant.getTenantId());
                }}))
        );
        List<GoodsSpecification> goodsSpecificationList = goodsSpecificationService.list(Wrappers.<GoodsSpecification>lambdaQuery()
                .in(GoodsSpecification::getTenantId, new ArrayList<String>() {{
                    add(BladeConstant.ADMIN_TENANT_ID);
                    add(merchant.getTenantId());
                }})
        );
        List<GoodsSpecificationData> goodsSpecificationDataList = goodsSpecificationDataService.list();

        List<GoodsCategoryVo> goodsCategoryVoList = new ArrayList<>();
        for (GoodsCategory goodsCategory : goodsCategoryList) {
            GoodsCategoryVo goodsCategoryVO = BeanUtil.copy(goodsCategory, GoodsCategoryVo.class);

            List<GoodsVo> goodsVoList = new ArrayList<>();
            for (Goods goods : goodsList) {
                GoodsVo goodsVo = BeanUtil.copy(goods, GoodsVo.class);
                Long[] goodsCategoryIdList = Func.toLongArray(goods.getCategoryId());

                if (Func.contains(goodsCategoryIdList, goodsCategory.getId())) {
                    Long[] goodsLabelIdList = Func.toLongArray(goodsVo.getLabelId());
                    Long[] goodsSpecificationIdList = Func.toLongArray(goodsVo.getSpecificationId());

                    List<GoodsLabel> goodsLabels = new ArrayList<>();
                    for (GoodsLabel goodsLabel : goodsLabelList) {
                        if (Func.contains(goodsLabelIdList, goodsLabel.getId())) {
                            goodsLabels.add(goodsLabel);
                        }
                    }

                    List<GoodsSpecificationVo> goodsSpecificationVoList = new ArrayList<>();
                    for (GoodsSpecification goodsSpecification : goodsSpecificationList) {
                        if (Func.contains(goodsSpecificationIdList, goodsSpecification.getId())) {
                            GoodsSpecificationVo goodsSpecificationVo = BeanUtil.copy(goodsSpecification, GoodsSpecificationVo.class);

                            List<GoodsSpecificationData> goodsSpecificationDataArrayList = new ArrayList<>();
                            for (GoodsSpecificationData goodsSpecificationData : goodsSpecificationDataList) {
                                if (goodsSpecificationData.getGoodsSpecificationId().equals(goodsSpecification.getId())) {
                                    goodsSpecificationDataArrayList.add(goodsSpecificationData);
                                }
                            }
                            goodsSpecificationVo.setGoodsSpecificationDataList(goodsSpecificationDataArrayList);
                            goodsSpecificationVoList.add(goodsSpecificationVo);
                        }
                    }

                    goodsVo.setIsSingle(Func.isEmpty(goods.getSpecificationId()));

                    goodsVo.setGoodsLabelList(goodsLabels);
                    goodsVo.setGoodsSpecificationList(goodsSpecificationVoList);
                    goodsVoList.add(goodsVo);
                }
            }
            goodsCategoryVO.setGoodsList(goodsVoList);
            goodsCategoryVoList.add(goodsCategoryVO);
        }

        return R.data(goodsCategoryVoList);
    }
}
